<?php
/**
* polymerinfo.php
*
* Create on 2016-8-5
* Create by TT
*/
namespace haibao\cms\data;
class PolymerInfo extends \haibao\cms\data\BaseMysql {
	public function __construct() {
		parent::__construct ( '\haibao\cms\model\data\PolymerInfo' );
	}
	
	/**
	 * 操作聚合记录-文章
	 */
	public function processPolymerInfoArticle($articleModel,$published = \haibao\cms\model\data\Article::PUBLISHED_NO){
		if($published == \haibao\cms\model\data\Article::PUBLISHED_YES && $articleModel->Level >= 1){
			$polymarInfoModel = $this->getPolymerInfo($articleModel->Id, \haibao\cms\model\data\PolymerInfo::OBJECT_TYPE_ARTICLE);
			if($polymarInfoModel){
				//修改
				$tagIdsStr = '';
				$tagIdsStrArr = array();
				if(!empty($articleModel->ArticleTag)){
					foreach($articleModel->ArticleTag as $articleTag){
						if($articleTag->IsPrimaiy == 1){
							$primaiyTagId = $articleTag->TagId;
							$parentTagId = $articleTag->ParentId;
						}
						$tagIdsStr .= '|' .$articleTag->TagTree;
						$tagIdsStrArr[] = $articleTag->TagTree;
					}
	
					$tagIdsStr = substr($tagIdsStr, 1);
				}
				$arrSet = array();
				$arrSet['UpdateType'] = $polymarInfoModel::UPDATE_TYPE_TITLE;
				$arrSet['Level'] = $articleModel->Level;
				if($polymarInfoModel->PrimaiyTagId != $primaiyTagId){
					$arrSet['PrimaiyTagId'] = $primaiyTagId;
					$arrSet['ParentTagId'] = $parentTagId;
				}
				if($articleModel->Title != $polymarInfoModel->Title || $articleModel->Summary != $polymarInfoModel->Summary){
					$arrSet['Title'] = $articleModel->Title;
					$arrSet['Summary'] = $articleModel->Summary;
				}
	
				if($tagIdsStr != $polymarInfoModel->TagList){
					$oldTagIdsStrArr = explode('|', $polymarInfoModel->TagList);
					$tagIds = array_diff($tagIdsStrArr,$oldTagIdsStrArr);
					if(empty($tagIds)){
						$tagIds = array_diff($oldTagIdsStrArr,$tagIdsStrArr);
					}
					if($tagIds){
						$arrSet['UpdateType'] = $polymarInfoModel::UPDATE_TYPE_TAG;
						$arrSet['TagList'] = $tagIdsStr;
						$arrSet['OldTagList'] = $polymarInfoModel->TagList;
					}
				}
				if($polymarInfoModel->Status == $polymarInfoModel::STATUS_OK || $polymarInfoModel->Status == $polymarInfoModel::STATUS_DEL){
					$arrSet['Status'] = $polymarInfoModel::STATUS_EDIT;
				}
				if(!empty($arrSet)){
					$this->updatePolymarInfo($articleModel->Id,$polymarInfoModel::OBJECT_TYPE_ARTICLE,$arrSet);
				}
			} else {
				//添加
				$this->addPolymerInfoArticle($articleModel);
			}
		}
	}
	
	/**
	 * 操作聚合记录-图集
	 */
	public function processPolymerInfoImage($imageCollectiveModel,$published = \haibao\cms\model\data\imagecollective\ImageCollective::STATUS_NO_PUBLISH){
		if($published == \haibao\cms\model\data\imagecollective\ImageCollective::STATUS_YES_PUBLISH && $imageCollectiveModel->Level >= 1){
			$polymarInfoModel = $this->getPolymerInfo($imageCollectiveModel->Id, \haibao\cms\model\data\PolymerInfo::OBJECT_TYPE_IMAGE);
			if($polymarInfoModel){
				//修改
				$tagIdsStr = '';
				$tagIdsStrArr = array();
				if(!empty($imageCollectiveModel->ImageCollectiveTag)){
					foreach($imageCollectiveModel->ImageCollectiveTag as $tag){
						if($tag['isPrimary'] == 1){
							$primaiyTagId = $tag['tagId'];
							$parentTagId = $tag['parentId'];
						}
						$tagIdsStr .= '|' .$tag['tagTree'];
						$tagIdsStrArr[] = $tag['tagTree'];
					}
	
					$tagIdsStr = substr($tagIdsStr, 1);
				}
				$arrSet = array();
				$arrSet['UpdateType'] = $polymarInfoModel::UPDATE_TYPE_TITLE;
				$arrSet['Level'] = $imageCollectiveModel->Level;
				if($polymarInfoModel->PrimaiyTagId != $primaiyTagId){
					$arrSet['PrimaiyTagId'] = $primaiyTagId;
					$arrSet['ParentTagId'] = $parentTagId;
				}
				if($imageCollectiveModel->Title != $polymarInfoModel->Title || $imageCollectiveModel->Description != $polymarInfoModel->Summary){
					$arrSet['Title'] = $imageCollectiveModel->Title;
					$arrSet['Summary'] = $imageCollectiveModel->Description;
				}
	
				if($tagIdsStr != $polymarInfoModel->TagList){
					$oldTagIdsStrArr = explode('|', $polymarInfoModel->TagList);
					$tagIds = array_diff($tagIdsStrArr,$oldTagIdsStrArr);
					if(empty($tagIds)){
						$tagIds = array_diff($oldTagIdsStrArr,$tagIdsStrArr);
					}
					if($tagIds){
						$arrSet['UpdateType'] = $polymarInfoModel::UPDATE_TYPE_TAG;
						$arrSet['TagList'] = $tagIdsStr;
						$arrSet['OldTagList'] = $polymarInfoModel->TagList;
					}
				}
				if($polymarInfoModel->Status == $polymarInfoModel::STATUS_OK || $polymarInfoModel->Status == $polymarInfoModel::STATUS_DEL){
					$arrSet['Status'] = $polymarInfoModel::STATUS_EDIT;
				}
				if(!empty($arrSet)){
					$this->updatePolymarInfo($imageCollectiveModel->Id,$polymarInfoModel::OBJECT_TYPE_IMAGE,$arrSet);
				}
			} else {
				//添加
				$this->addPolymerInfoImage($imageCollectiveModel);
			}
		}
	}
	
	/**
	 * 操作聚合记录-视频
	 */
	public function processPolymerInfoVideo($videoModel,$tagIdList = array(),$isAdd = false){
		if($isAdd){
			$this->addPolymerInfoVideo($videoModel,$tagIdList);
		} else {
			$polymarInfoModel = $this->getPolymerInfo($videoModel->Id, \haibao\cms\model\data\PolymerInfo::OBJECT_TYPE_VIDEO);
			if($polymarInfoModel){
				$tagIdsStr = '';
				$tagIdsStrArr = array();
				if($tagIdList){
					$tagIdsStr = implode('|', array_values($tagIdList));
					$tagIdsStrArr = array_values($tagIdList);
				}
				 
				$arrSet = array();
				if($videoModel->Title != $polymarInfoModel->Title || $videoModel->Description != $polymarInfoModel->Summary){
					$arrSet['UpdateType'] = $polymarInfoModel::UPDATE_TYPE_TITLE;
					$arrSet['Title'] = $videoModel->Title;
					$arrSet['Summary'] = $videoModel->Description;
				}
				 
				if($tagIdsStr != $polymarInfoModel->TagList){
					$oldTagIdsStrArr = explode('|', $polymarInfoModel->TagList);
					$tagIds = array_diff($tagIdsStrArr,$oldTagIdsStrArr);
					if(empty($tagIds)){
						$tagIds = array_diff($oldTagIdsStrArr,$tagIdsStrArr);
					}
					if($tagIds){
						$arrSet['UpdateType'] = $polymarInfoModel::UPDATE_TYPE_TAG;
						$arrSet['TagList'] = $tagIdsStr;
						$arrSet['OldTagList'] = $polymarInfoModel->TagList;
					}
				}
				if($polymarInfoModel->Status == $polymarInfoModel::STATUS_OK || $polymarInfoModel->Status == $polymarInfoModel::STATUS_DEL){
					$arrSet['Status'] = $polymarInfoModel::STATUS_EDIT;
				}
				if(!empty($arrSet)){
					$this->updatePolymarInfo($videoModel->Id,$polymarInfoModel::OBJECT_TYPE_VIDEO,$arrSet);
				}
			} else {
				//添加
				$this->addPolymerInfoVideo($videoModel,$tagIdList);
			}
		}
		 
	}
	
	//操作聚合记录
	public function processPolymerInfoFeature($featureId,$polymerInfoDataVal,$tagIdsStr,$tagIdsStrArr){
		$polymarInfoModel = $this->getPolymerInfo($featureId, $polymerInfoDataVal['ObjectType']);
		if($polymarInfoModel){
			$arrSet = array();
			if($polymerInfoDataVal['Title'] != $polymarInfoModel->Title || $polymerInfoDataVal['Summary'] != $polymarInfoModel->Summary){
				$arrSet['UpdateType'] = $polymarInfoModel::UPDATE_TYPE_TITLE;
				$arrSet['Title'] = $polymerInfoDataVal['Title'];
				$arrSet['Summary'] = $polymerInfoDataVal['Summary'];
			}
				
			if($tagIdsStr != $polymarInfoModel->TagList){
				$oldTagIdsStrArr = explode('|', $polymarInfoModel->TagList);
				$tagIds = array_diff($tagIdsStrArr,$oldTagIdsStrArr);
				if(empty($tagIds)){
					$tagIds = array_diff($oldTagIdsStrArr,$tagIdsStrArr);
				}
				if($tagIds){
					$arrSet['UpdateType'] = $polymarInfoModel::UPDATE_TYPE_TAG;
					$arrSet['TagList'] = $tagIdsStr;
					$arrSet['OldTagList'] = $polymarInfoModel->TagList;
				}
			}
			if($polymarInfoModel->Status == $polymarInfoModel::STATUS_OK || $polymarInfoModel->Status == $polymarInfoModel::STATUS_DEL){
				$arrSet['Status'] = $polymarInfoModel::STATUS_EDIT;
			}
			if(!empty($arrSet)){
				$this->updatePolymarInfo($featureId,$polymerInfoDataVal['ObjectType'],$arrSet);
			}
		} else {
			$this->addPolymerInfoFeature($featureId, $polymerInfoDataVal,$tagIdsStr);
		}
	}
	
	/**
	 * 查询聚合记录
	 * @param unknown_type $objectId
	 * @param unknown_type $objectType
	 */
	public function getPolymerInfo($objectId,$objectType){
		$polymarInfoModel = null;
		$sql = "select * from PolymerInfo where ObjectId = %s and ObjectType = %s";
		$result = $this->query($sql,array($objectId,$objectType));
		while($result->fetch_assoc()){
			$polymarInfoModel = new \haibao\cms\model\data\PolymerInfo();
			$polymarInfoModel->Title = $result->getData('Title');
			$polymarInfoModel->Summary = $result->getData('Summary');
			$polymarInfoModel->TagList = $result->getData('TagList');
			$polymarInfoModel->PrimaiyTagId = $result->getData('PrimaiyTagId');
			$polymarInfoModel->Status = $result->getData('Status');
		}
	
		return $polymarInfoModel;
	}
	
	/**
	 * 修改聚合记录
	 * @param int $objectId
	 * @param int $objectType
	 * @param array $arrSet
	 */
	public function updatePolymarInfo($objectId,$objectType,$arrSet){
		$setStr = '';
		$setValArr = array();
		foreach ($arrSet as $k=>$v){
			$setStr .= ',' . $k . '=%s';
			$setValArr[] = $v;
		}
		$sql = "update PolymerInfo set ".substr($setStr, 1)." where ObjectId=%s and ObjectType=%s";
		$setValArr[] = $objectId;
		$setValArr[] = $objectType;
		$this->query ($sql,$setValArr);
	}
	
	/**
	 * 添加聚合记录
	 * @param unknown_type $articleModel
	 */
	private function addPolymerInfoArticle($articleModel){
		$primaiyTagId = 0;
		$parentTagId = 0;
		$tagIdsStr = '';
		if(!empty($articleModel->ArticleTag)){
			foreach($articleModel->ArticleTag as $articleTag){
				if($articleTag->IsPrimaiy == 1){
					$primaiyTagId = $articleTag->TagId;
					$parentTagId = $articleTag->ParentId;
				}
				$tagIdsStr .= '|' .$articleTag->TagTree;
			}
	
			$polymarInfoModel = new \haibao\cms\model\data\PolymerInfo();
			$polymarInfoModel->ObjectId = $articleModel->Id;
			$polymarInfoModel->ObjectType = $polymarInfoModel::OBJECT_TYPE_ARTICLE;
			$polymarInfoModel->Title = $articleModel->Title;
			$polymarInfoModel->Summary = $articleModel->Summary;
			$polymarInfoModel->PublishId = $articleModel->PublishId;
			$polymarInfoModel->PublishTime = $articleModel->PublishTime;
			$polymarInfoModel->PrimaiyTagId = $primaiyTagId;
			$polymarInfoModel->ParentTagId = $parentTagId;
			$polymarInfoModel->TagList = substr($tagIdsStr, 1);
			$polymarInfoModel->OldTagList = substr($tagIdsStr, 1);
			$polymarInfoModel->Status = $polymarInfoModel::STATUS_ADD;
			$polymarInfoModel->CreateTime = $articleModel->PublishTime;
			$polymarInfoModel->UpdateType = $polymarInfoModel::UPDATE_TYPE_TITLE;
			$polymarInfoModel->Level = $articleModel->Level;
			$this->add($polymarInfoModel);
		}
	}
	


	/**
	 * 添加聚合记录
	 * @param unknown_type $imageCollectiveModel
	 */
	private function addPolymerInfoImage($imageCollectiveModel){
		$primaiyTagId = 0;
		$parentTagId = 0;
		$tagIdsStr = '';
		if(!empty($imageCollectiveModel->ImageCollectiveTag)){
			foreach($imageCollectiveModel->ImageCollectiveTag as $tag){
				if($tag['isPrimary'] == 1){
					$primaiyTagId = $tag['tagId'];
					$parentTagId = $tag['parentId'];
				}
				$tagIdsStr .= '|' .$tag['tagTree'];
			}
	
			$polymarInfoModel = new \haibao\cms\model\data\PolymerInfo();
			$polymarInfoModel->ObjectId = $imageCollectiveModel->Id;
			$polymarInfoModel->ObjectType = $polymarInfoModel::OBJECT_TYPE_IMAGE;
			$polymarInfoModel->Title = $imageCollectiveModel->Title;
			$polymarInfoModel->Summary = $imageCollectiveModel->Description;
			$polymarInfoModel->PublishId = $imageCollectiveModel->Id;
			$polymarInfoModel->PublishTime = $imageCollectiveModel->PublishTime;
			$polymarInfoModel->PrimaiyTagId = $primaiyTagId;
			$polymarInfoModel->ParentTagId = $parentTagId;
			$polymarInfoModel->TagList = substr($tagIdsStr, 1);
			$polymarInfoModel->OldTagList = substr($tagIdsStr, 1);
			$polymarInfoModel->Status = $polymarInfoModel::STATUS_ADD;
			$polymarInfoModel->CreateTime = $imageCollectiveModel->PublishTime;
			$polymarInfoModel->UpdateType = $polymarInfoModel::UPDATE_TYPE_TITLE;
			$polymarInfoModel->Level = $imageCollectiveModel->Level;
			$this->add($polymarInfoModel);
		}
	}

	private function addPolymerInfoVideo($videoModel,$tagIdList = array()){
		if(!empty($videoModel->VideorelTagModel)){
			$tagStr = '';
			if($tagIdList){
				$tagStr = implode('|', array_values($tagIdList));
			}
	
			$polymarInfoModel = new \haibao\cms\model\data\PolymerInfo();
			$polymarInfoModel->ObjectId = $videoModel->Id;
			$polymarInfoModel->ObjectType = $polymarInfoModel::OBJECT_TYPE_VIDEO;
			$polymarInfoModel->Title = $videoModel->Title;
			$polymarInfoModel->Summary = $videoModel->Description;
			$polymarInfoModel->PublishId = $videoModel->Id;
			$polymarInfoModel->PublishTime = $videoModel->PublishDate;
			$polymarInfoModel->PrimaiyTagId = 0;
			$polymarInfoModel->ParentTagId = 0;
			$polymarInfoModel->TagList = $tagStr;
			$polymarInfoModel->OldTagList = $tagStr;
			$polymarInfoModel->Status = $polymarInfoModel::STATUS_ADD;
			$polymarInfoModel->CreateTime = $videoModel->PublishDate;
			$polymarInfoModel->UpdateType = $polymarInfoModel::UPDATE_TYPE_TITLE;
			$polymarInfoModel->Level = 1;
			$this->add($polymarInfoModel);
		}
	}

	/**
	 * 添加聚合记录
	 * @param unknown_type $articleModel
	 */
	private function addPolymerInfoFeature($featureId,$polymerInfoDataVal,$tagIdsStr){
		$featureData = new \haibao\cms\data\FeaturePublish();
		$polymarInfoModel = new \haibao\cms\model\data\PolymerInfo();
		$polymarInfoModel->ObjectId = $featureId;
		$polymarInfoModel->ObjectType = $polymerInfoDataVal['ObjectType'];
		$polymarInfoModel->Title = $polymerInfoDataVal['Title'];
		$polymarInfoModel->Summary = $polymerInfoDataVal['Summary'];
		$polymarInfoModel->PublishId = $featureId;
		$polymarInfoModel->PublishTime = $polymerInfoDataVal['PublishTime'];
		$polymarInfoModel->PrimaiyTagId = 0;
		$polymarInfoModel->ParentTagId = 0;
		$polymarInfoModel->TagList = $tagIdsStr;
		$polymarInfoModel->OldTagList = $tagIdsStr;
		$polymarInfoModel->Status = $polymarInfoModel::STATUS_ADD;
		$polymarInfoModel->CreateTime = $polymerInfoDataVal['PublishTime'];
		$polymarInfoModel->UpdateType = \haibao\cms\model\data\PolymerInfo::UPDATE_TYPE_TITLE;
		$polymarInfoModel->Level = 1;
		$featureData->add($polymarInfoModel);
	}

}
